package com.un.ebs.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.inventory.domain.Warehouse;
import com.un.ebs.inventory.mapper.WarehouseMapper;
import com.un.ebs.inventory.service.WarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.un.ebs.inventory.dto.*;

import java.awt.image.WritableRaster;
import java.util.List;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2020-05-24
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Override
    public void add(WarehouseAddDto req) {
        WarehouseUpdateDto obj = new WarehouseUpdateDto();
        BeanUtils.copyProperties(req, obj);
        save(obj);
    }

    @Override
    public void update(WarehouseUpdateDto req) {
        save(req);
    }

    private void save(WarehouseUpdateDto req) {
        Warehouse obj = StringUtils.isNullOrEmpty(req.getId()) ? new Warehouse() : getById(req.getId());
        obj.setParentId(req.getParentId());
        obj.setCode(req.getCode());
        obj.setName(req.getName());
        obj.setAddress(req.getAddress());
        obj.setTel(req.getTel());
        obj.setRemark(req.getRemark());
        if (req.getIsReceive() == null) {
            obj.setIsReceive(false);
        } else {
            obj.setIsReceive(req.getIsReceive());
        }
        if (req.getIsDeliver() == null) {
            obj.setIsDeliver(false);
        } else {
            obj.setIsDeliver(req.getIsDeliver());
        }
        if (req.getIsStore() == null) {
            obj.setIsStore(false);
        } else {
            obj.setIsStore(req.getIsStore());
        }
        if (req.getIsProducer() == null) {
            obj.setIsProducer(false);
        } else {
            obj.setIsProducer(req.getIsProducer());
        }
        if (req.getIsTemp() == null) {
            req.setIsTemp(false);
        } else {
            obj.setIsTemp(req.getIsTemp());
        }
        if (req.getIsBad() == null) {
            obj.setIsBad(false);
        } else {
            obj.setIsBad(req.getIsBad());
        }
        if (req.getIsProduct() == null) {
            obj.setIsProduct(false);
        } else {
            obj.setIsProduct(req.getIsProduct());
        }
        if (req.getIsMaterial() == null) {
            obj.setIsMaterial(false);
        } else {
            obj.setIsMaterial(req.getIsMaterial());
        }
        obj.insertOrUpdate();
    }

    @Override
    public Warehouse queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public IPage<Warehouse> query(String searchText, String parentId, Integer pageSize, Integer pageIndex) {
        Page<Warehouse> page = new Page(pageIndex, pageSize);
        LambdaQueryWrapper<Warehouse> lq = Wrappers.lambdaQuery();
        lq.eq(Warehouse::getIsDelete, false);
        if (!StringUtils.isNullOrEmpty(parentId)) {
            lq.eq(Warehouse::getParentId, parentId);
        }
        if (!StringUtils.isNullOrEmpty(searchText)) {
            lq.like(Warehouse::getName, searchText).or()
                    .like(Warehouse::getCode, searchText);
        }
        lq.orderByAsc(Warehouse::getCode);
        return baseMapper.selectPage(page, lq);
    }

    @Override
    public List<Warehouse> queryMatierial() {
        LambdaQueryWrapper<Warehouse> lq = Wrappers.lambdaQuery();
        lq.eq(Warehouse::getIsMaterial, true);
        lq.eq(Warehouse::getIsStore, true);
        return baseMapper.selectList(lq);
    }

    @Override
    public List<Warehouse> queryProduct() {
        LambdaQueryWrapper<Warehouse> lq = Wrappers.lambdaQuery();
        lq.eq(Warehouse::getIsProduct, true);
        lq.eq(Warehouse::getIsStore, true);
        return baseMapper.selectList(lq);
    }

    @Override
    public void changeParent(String id, String parentId) {
        Warehouse obj = getById(id);
        obj.setParentId(parentId);
        getBaseMapper().updateById(obj);
    }

    @Override
    public List<Warehouse> queryByParentId(String parentId) {
        QueryWrapper<Warehouse> wrapper = Wrappers.query();
        wrapper.ge("parent_id", parentId);
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    public IPage<Warehouse> queryPageByParentId(String parentId, int pageSize, int pageIndex) {
        Page<Warehouse> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<Warehouse> wrapper = Wrappers.query();
        wrapper.ge("parent_id", parentId);
        return getBaseMapper().selectPage(page, wrapper);
    }

    @Override
    public void delete(String id) {
        getBaseMapper().deleteById(id);
    }

}